package cn.felord.debezium.service.impl;

import cn.felord.debezium.common.WarnDist;
import cn.felord.debezium.dao.*;
import cn.felord.debezium.entity.*;
import cn.felord.debezium.service.DetectService;
import cn.felord.debezium.utils.AuToSend;
import cn.felord.debezium.utils.CRC16Checksum;
import cn.felord.debezium.utils.NumberUtils;
import cn.felord.debezium.utils.String2AcsII16;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static cn.felord.debezium.common.Watch.*;
import static cn.felord.debezium.utils.NumberUtils.HexStrToByteArray;
import static cn.felord.debezium.utils.String2AcsII16.*;

/**
 * 单纯报警只存子表信息  warnType = 'FFFF'
 *
 * 加料 主表存 查询两小时内主表 有则取id 子表用masterId,无则新增，再留Id
 * 子表存信息 报警存报警类型和报警信息 无报警，只存报警类型。报警信息为空显示用，自定义，原则上应该显示未报警。
 */
@Service
@Slf4j
public class TcpActServiceIml {
    public static final String KDTC = "KDTC";
    public static final BigDecimal bigDecimal100 = new BigDecimal(100);
    @Autowired
    DetectionRlMapper detectionRlMapper;
    @Autowired
    DetectionRlEjrlMapper detectionRlEjrlMapper;
    @Autowired
    SmelterMelterStandardMapper smelterMelterStandardMapper;
    @Autowired
    ScaleEquipmentMamageMapper scaleEquipmentMamageMapper;
    @Autowired
    AuToSend auToSend;
    @Autowired
    DetectService detectService;
    @Autowired
    ScaleAlarmMasterMapper scaleAlarmMasterMapper;
    @Autowired
    ScaleAlarmSubMapper scaleAlarmSubMapper;
    @Autowired
    EquipmentConnectInfoMapper equipmentConnectInfoMapper;
    @Autowired
    EquipmentInfoMapper equipmentInfoMapper;
    @Autowired
    EquipmentDetectionStandardMapper equipmentDetectionStandardMapper;
    @Autowired
    HSfMaterialsMapper hSfMaterialsMapper;
    @Autowired
    HSfAncillaryMateriasMapper hSfAncillaryMateriasMapper;

    /**
     * 产后  standard走标准里的球化硅含量
     * @param info
     * @param outputStream
     * @
     */
    public void updateAfterQhqDetectionStand(String info,IoSession outputStream)  {
        String sub = info.substring(18);//010002004304F904F6FF 设备编码开始 之前去掉
        String type = hexToString(sub.substring(0, 8));//设备类型
        log.info("进入设备设置 ，设备类型：" + type);
        String equipNum = sub.substring(8,16);
        byte[] byteArray = HexStrToByteArray("FE01100600000E4541FF");

//            outputStream.write(byteArray);
            sendMsg(outputStream,"FE01100600000E4541FF");
       // outputStream.flush();
        sub = sub.substring(16);//用完16截取，继续0开始 保留以后得字符串
        String preview = sub.substring(0, 8);
        sub = sub.substring(8);//用完截取，继续0开始
        String QHSi= hexToString(sub.substring(0,8));
        sub = sub.substring(8);//用完截取，继续0开始
        int QHSi_sorb = 0;
        sub = sub.substring(4);//用完截取，继续0开始
        int QHSi_eff = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始
        EquipmentDetectionStandard equipmentDetectionStandard = equipmentDetectionStandardMapper.selectEquipmentDetectionStandardByEquipCode(type + equipNum);
        if(equipmentDetectionStandard != null){
            equipmentDetectionStandard.setLable1(new BigDecimal(QHSi_eff).divide(bigDecimal100).toString());
            equipmentDetectionStandard.setCreateTime(new Date());
            equipmentDetectionStandardMapper.updateEquipmentDetectionStandard(equipmentDetectionStandard);
            log.info("更新球化铁水标准后Si%");
        }else {
            EquipmentDetectionStandard equipmentDetectionStandard1 = new EquipmentDetectionStandard();
            equipmentDetectionStandard1.setEquipType(type);
            equipmentDetectionStandard1.setEquipNum(equipNum);
            equipmentDetectionStandard1.setEquipCode(type + equipNum);
            equipmentDetectionStandard1.setUpdateTime(new Date());
            equipmentDetectionStandard1.setLable1(new BigDecimal(QHSi_eff).divide(bigDecimal100).toString());
            equipmentDetectionStandardMapper.insertEquipmentDetectionStandard(equipmentDetectionStandard1);
            log.info("更新球化铁水标准后Si%");
        }
    }
    public void queryAfterQhqDetectionStand(String info,IoSession outputStream)  {
        String sub = info.substring(14);//010002004304F904F6FF 设备编码开始 之前去掉
        String type = hexToString(sub.substring(0, 8));//设备类型
        log.info("进入设备设置 ，设备类型：" + type);
        String equipNum = sub.substring(8,16);
        String zjsStr16 = String2AcsII16.string2hex(KDTC) + equipNum + "00000000";
        EquipmentDetectionStandard equipmentDetectionStandard = equipmentDetectionStandardMapper.selectEquipmentDetectionStandardByEquipCode(type + equipNum);
        if(equipmentDetectionStandard == null){
            log.error(type + equipNum + "设备标准缺失！");
            return;
        }else {
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("QHSi");
            zjsStr16 = zjsStr16 + "0000";
            zjsStr16 = zjsStr16 + int10to16_4(new BigDecimal(equipmentDetectionStandard.getLable1()).multiply(bigDecimal100).intValue());
        }
        zjsStr16  = zjsStr16 + "00000000" + "00000000";
        int zjs = zjsStr16.length() / 2;// 不合格元素个数 * 4，再上位机收到光谱数转16进制
        String zjs16 = String2AcsII16.int10to16_2(zjs);

        String crc_str = "0103" + zjs16 + zjsStr16;
        String CRC_CHECK_CODE = CRC16Checksum.getCrc16ModBus(crc_str);//检验码

        StringBuffer sf = new StringBuffer();
        sf.append("FE");
        sf.append(crc_str);
        sf.append(CRC_CHECK_CODE);
        sf.append("FF");
        log.info("发送产后质检标准信息结果：" + sf.toString());
        byte[] byteArray = HexStrToByteArray(sf.toString());

//            outputStream.write(byteArray);
            sendMsg(outputStream,sf.toString());
       // outputStream.flush();
    }
    /**
     * 更细产前
     * @param info
     * @param outputStream
     * @
     */
    public void updateBeforeMaterias(String info,IoSession outputStream)  {
        String sub = info.substring(18);//010002004304F904F6FF 设备编码开始 之前去掉
        String type = hexToString(sub.substring(0, 8));//设备类型
        log.info("进入设备设置 ，设备类型：" + type);
        String equipNum = sub.substring(8,16);

        byte[] byteArray = HexStrToByteArray("FE01100500002A1A41FF");

//            outputStream.write(byteArray);
            sendMsg(outputStream,"FE01100400002CE4C0FF");
       // outputStream.flush();
        sub = sub.substring(16);//用完16截取，继续0开始 保留以后得字符串

        String preview = sub.substring(0, 8);
        sub = sub.substring(8);//用完截取，继续0开始

        String HLC = hexToString(sub.substring(0,8));
        sub = sub.substring(8);//用完截取，继续0开始
        int HLC_sorb = 0;
        sub = sub.substring(4);//用完截取，继续0开始
        int HLC_eff = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始

        String HLSi = hexToString(sub.substring(0,8));
        sub = sub.substring(8);//用完截取，继续0开始
        int HLSi_sorb = 0;
        sub = sub.substring(4);//用完截取，继续0开始
        int HLSi_eff = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始

        String FGC = hexToString(sub.substring(0,8));
        sub = sub.substring(8);//用完截取，继续0开始
        int FGC_sorb = 0;
        sub = sub.substring(4);//用完截取，继续0开始
        int FGC_eff = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始
        String FGSi = hexToString(sub.substring(0,8));
        sub = sub.substring(8);//用完截取，继续0开始
        int FGSi_sorb = 0;
        sub = sub.substring(4);//用完截取，继续0开始
        int FGSi_eff = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始

        String STC = hexToString(sub.substring(0,8));
        sub = sub.substring(8);//用完截取，继续0开始
        int STC_sorb = 0;
        sub = sub.substring(4);//用完截取，继续0开始
        int STC_eff = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始
        String STSi = hexToString(sub.substring(0,8));
        sub = sub.substring(8);//用完截取，继续0开始
        int STSi_sorb = 0;
        sub = sub.substring(4);//用完截取，继续0开始
        int STSi_eff = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始


        String BZC = hexToString(sub.substring(0,8));
        sub = sub.substring(8);//用完截取，继续0开始
        int BZC_sorb = 0;
        sub = sub.substring(4);//用完截取，继续0开始
        int BZC_eff = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始
        String BZSi = hexToString(sub.substring(0,8));
        sub = sub.substring(8);//用完截取，继续0开始
        int BZSi_sorb = 0;
        sub = sub.substring(4);//用完截取，继续0开始
        int BZSi_eff = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始
        /**
         * 辅料含量和吸收率都有
         */

        String SiCC = hexToString(sub.substring(0,8));
        sub = sub.substring(8);//用完截取，继续0开始
        int SiCC_sorb = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始
        int SiCC_eff = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始
        String SiCS = hexToString(sub.substring(0,8));
        sub = sub.substring(8);//用完截取，继续0开始
        int SiCS_sorb = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始
        int SiCS_eff = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始

        String SLC = hexToString(sub.substring(0,8));
        sub = sub.substring(8);//用完截取，继续0开始
        int SLC_sorb = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始
        int SLC_eff = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始
        String SLSi = hexToString(sub.substring(0,8));
        sub = sub.substring(8);//用完截取，继续0开始
        int SLSi_sorb = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始
        int SLSi_eff = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始

        String GFGC = hexToString(sub.substring(0,8));
        sub = sub.substring(8);//用完截取，继续0开始
        int GFGC_sorb = 0;
        sub = sub.substring(4);//用完截取，继续0开始
        int GFGC_eff = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始
        String GFGSi = hexToString(sub.substring(0,8));
        sub = sub.substring(8);//用完截取，继续0开始
        int GFGSi_sorb = 0;
        sub = sub.substring(4);//用完截取，继续0开始
        int GFGSi_eff = hex2Int(sub.substring(0,4));

        List<HSfMaterials> hSfMaterials = hSfMaterialsMapper
                .selectHSfMaterialsByEquipCode(type + equipNum);
        HashMap<String,HSfMaterials> hSfMaterialsHashMap = new HashMap<>();
        for(HSfMaterials ancillaryMaterias : hSfMaterials){
            hSfMaterialsHashMap.put(ancillaryMaterias.getmName(),ancillaryMaterias);
        }
        BigDecimal b100 = new BigDecimal(100);
        HSfMaterials HL = hSfMaterialsHashMap.get("HL");
        int k = 0;
        int j = 0;
        if(HL != null){
            HL.setMC(new BigDecimal(HLC_eff).divide(b100));
            HL.setmSi(new BigDecimal(HLSi_eff).divide(b100));
            HL.setUpdateTime(new Date());
            k = k + hSfMaterialsMapper.updateHSfMaterials(HL);
        }else {
            HSfMaterials HLNEW = new HSfMaterials();
            HLNEW.setEquipType(type);
            HLNEW.setEquipNum(equipNum);
            HLNEW.setEquipCode(type + equipNum);
            HLNEW.setmName("HL");
            HLNEW.setMC(new BigDecimal(HLC_eff).divide(b100));
            HLNEW.setmSi(new BigDecimal(HLSi_eff).divide(b100));
            HLNEW.setCreateTime(new Date());
            j = j + hSfMaterialsMapper.insertHSfMaterials(HLNEW);
        }

        HSfMaterials FGCO = hSfMaterialsHashMap.get("FG");
        if(FGCO != null){
            FGCO.setMC(new BigDecimal(FGC_eff).divide(b100));
            FGCO.setmSi(new BigDecimal(FGSi_eff).divide(b100));
            FGCO.setUpdateTime(new Date());
            k = k + hSfMaterialsMapper.updateHSfMaterials(FGCO);
        }else {
            HSfMaterials FGCONEW = new HSfMaterials();
            FGCONEW.setEquipType(type);
            FGCONEW.setEquipNum(equipNum);
            FGCONEW.setEquipCode(type + equipNum);
            FGCONEW.setmName("FG");
            FGCONEW.setRemark(WarnDist.getDistMaterias().get("FG"));
            FGCONEW.setMC(new BigDecimal(FGC_eff).divide(b100));
            FGCONEW.setmSi(new BigDecimal(FGSi_eff).divide(b100));
            FGCONEW.setCreateTime(new Date());
            j = j + hSfMaterialsMapper.insertHSfMaterials(FGCONEW);
        }

        HSfMaterials ST = hSfMaterialsHashMap.get("ST");
        if(ST != null){
            ST.setMC(new BigDecimal(STC_eff).divide(b100));
            ST.setmSi(new BigDecimal(STSi_eff).divide(b100));
            ST.setUpdateTime(new Date());
            k = k + hSfMaterialsMapper.updateHSfMaterials(ST);
        }else {
            HSfMaterials STNEW = new HSfMaterials();
            STNEW.setEquipType(type);
            STNEW.setEquipNum(equipNum);
            STNEW.setEquipCode(type + equipNum);
            STNEW.setmName("ST");
            STNEW.setRemark(WarnDist.getDistMaterias().get("ST"));//生铁
            STNEW.setMC(new BigDecimal(STC_eff).divide(b100));
            STNEW.setmSi(new BigDecimal(STSi_eff).divide(b100));
            j = j + hSfMaterialsMapper.insertHSfMaterials(STNEW);
        }

        EquipmentDetectionStandard equipmentDetectionStandard = equipmentDetectionStandardMapper
                .selectEquipmentDetectionStandardByEquipCode(type + equipNum);
        if(equipmentDetectionStandard != null){
            equipmentDetectionStandard.setC(new BigDecimal(BZC_eff).divide(bigDecimal100).toString());
            equipmentDetectionStandard.setSi(new BigDecimal(BZSi_eff).divide(bigDecimal100).toString());
            equipmentDetectionStandard.setUpdateTime(new Date());
            k = k +  equipmentDetectionStandardMapper.updateEquipmentDetectionStandard(equipmentDetectionStandard);
        }else {
            EquipmentDetectionStandard equipmentDetectionStandard1 = new EquipmentDetectionStandard();
            equipmentDetectionStandard1.setEquipType(type);
            equipmentDetectionStandard1.setEquipNum(equipNum);
            equipmentDetectionStandard1.setEquipCode(type + equipNum);
            equipmentDetectionStandard1.setCreateTime(new Date());
            equipmentDetectionStandard1.setC(new BigDecimal(BZC_eff).divide(bigDecimal100).toString());
            equipmentDetectionStandard1.setSi(new BigDecimal(BZSi_eff).divide(bigDecimal100).toString());
            j = j + equipmentDetectionStandardMapper.insertEquipmentDetectionStandard(equipmentDetectionStandard1);
        }

        List<HSfAncillaryMaterias> hSfAncillaryMaterias = hSfAncillaryMateriasMapper
                .selectHSfAncillaryMateriasByEquipCode(type + equipNum);
        HashMap<String,HSfAncillaryMaterias> hSfAncillaryMateriasHashMap = new HashMap<>();
        for(HSfAncillaryMaterias ancillaryMaterias : hSfAncillaryMaterias){
            hSfAncillaryMateriasHashMap.put(ancillaryMaterias.geteName(),ancillaryMaterias);
        }
        /**
         * 辅料 四个
         */
        //SiCC 【碳化硅】 SiCS【硫化硅】   SLC[增碳剂] SLSi[硅粒]
        HSfAncillaryMaterias siCC = hSfAncillaryMateriasHashMap.get("SiCC");
        if(siCC != null){
            siCC.seteAbsorb(new BigDecimal(SiCC_sorb).divide(b100));
            siCC.seteEffective(new BigDecimal(SiCC_eff).divide(b100));
            siCC.setUpdateTime(new Date());
            k = k +  hSfAncillaryMateriasMapper.updateHSfAncillaryMaterias(siCC);
        }else {
            HSfAncillaryMaterias siCCnew = new HSfAncillaryMaterias();
            siCCnew.setCreateTime(new Date());
            siCCnew.seteName("SiCC");
            siCCnew.setRemark(WarnDist.getDistYs().get("SiCC"));//碳化硅(C)
            siCCnew.setEquipType(type);
            siCCnew.setEquipNum(equipNum);
            siCCnew.setEquipCode(type + equipNum);
            siCCnew.seteAbsorb(new BigDecimal(SiCC_sorb).divide(b100));
            siCCnew.seteEffective(new BigDecimal(SiCC_eff).divide(b100));
            j = j + hSfAncillaryMateriasMapper.insertHSfAncillaryMaterias(siCCnew);
        }
        HSfAncillaryMaterias siCS = hSfAncillaryMateriasHashMap.get("SiCS");
        if(siCS != null){
            siCS.seteAbsorb(new BigDecimal(SiCS_sorb).divide(b100));
            siCS.seteEffective(new BigDecimal(SiCS_eff).divide(b100));
            siCS.setUpdateTime(new Date());
            k = k +  hSfAncillaryMateriasMapper.updateHSfAncillaryMaterias(siCS);
        }else {
            HSfAncillaryMaterias siCSnew = new HSfAncillaryMaterias();
            siCSnew.setCreateTime(new Date());
            siCSnew.seteName("SiCS");
            siCSnew.setRemark(WarnDist.getDistYs().get("SiCS"));//碳化硅(Si)
            siCSnew.setEquipType(type);
            siCSnew.setEquipNum(equipNum);
            siCSnew.setEquipCode(type + equipNum);
            siCSnew.seteAbsorb(new BigDecimal(SiCS_sorb).divide(b100));
            siCSnew.seteEffective(new BigDecimal(SiCS_eff).divide(b100));
            j = j + hSfAncillaryMateriasMapper.insertHSfAncillaryMaterias(siCSnew);
        }
        HSfAncillaryMaterias slc = hSfAncillaryMateriasHashMap.get("SLC");
        if(slc != null){
            slc.seteAbsorb(new BigDecimal(SLC_sorb).divide(b100));
            slc.seteEffective(new BigDecimal(SLC_eff).divide(b100));
            slc.setUpdateTime(new Date());
            k = k +  hSfAncillaryMateriasMapper.updateHSfAncillaryMaterias(slc);
        }else {
            HSfAncillaryMaterias slcnew = new HSfAncillaryMaterias();
            slcnew.setCreateTime(new Date());
            slcnew.seteName("SLC");
            slcnew.setRemark(WarnDist.getDistYs().get("SLC"));//增碳剂
            slcnew.setEquipType(type);
            slcnew.setEquipNum(equipNum);
            slcnew.setEquipCode(type + equipNum);
            slcnew.seteAbsorb(new BigDecimal(SLC_sorb).divide(b100));
            slcnew.seteEffective(new BigDecimal(SLC_eff).divide(b100));
            j = j + hSfAncillaryMateriasMapper.insertHSfAncillaryMaterias(slcnew);
        }
        HSfAncillaryMaterias slSi = hSfAncillaryMateriasHashMap.get("SLSi");
        if(slSi != null){
            slSi.seteAbsorb(new BigDecimal(SLSi_sorb).divide(b100));
            slSi.seteEffective(new BigDecimal(SLSi_eff).divide(b100));
            slSi.setUpdateTime(new Date());
            k = k +  hSfAncillaryMateriasMapper.updateHSfAncillaryMaterias(slSi);
        }else {
            HSfAncillaryMaterias slSinew = new HSfAncillaryMaterias();
            slSinew.setCreateTime(new Date());
            slSinew.seteName("SLSi");
            slSinew.setRemark(WarnDist.getDistYs().get("SLSi"));//硅粒
            slSinew.setEquipType(type);
            slSinew.setEquipNum(equipNum);
            slSinew.setEquipCode(type + equipNum);
            slSinew.seteAbsorb(new BigDecimal(SLSi_sorb).divide(b100));
            slSinew.seteEffective(new BigDecimal(SLSi_eff).divide(b100));
            j = j + hSfAncillaryMateriasMapper.insertHSfAncillaryMaterias(slSinew);
        }

        /**
         * 高铬废钢
         */
        HSfMaterials GFG = hSfMaterialsHashMap.get("GFG");
        if(GFG != null){
            GFG.setMC(new BigDecimal(GFGC_eff).divide(b100));
            GFG.setmSi(new BigDecimal(GFGSi_eff).divide(b100));
            k = k + hSfMaterialsMapper.updateHSfMaterials(GFG);
        }else {
            HSfMaterials GFGNEW = new HSfMaterials();
            GFGNEW.setEquipType(type);
            GFGNEW.setEquipNum(equipNum);
            GFGNEW.setEquipCode(type + equipNum);
            GFGNEW.setmName("GFG");
            GFGNEW.setRemark(WarnDist.getDistMaterias().get("GFG"));
            GFGNEW.setMC(new BigDecimal(GFGC_eff).divide(b100));
            GFGNEW.setmSi(new BigDecimal(GFGSi_eff).divide(b100));
            j = j + hSfMaterialsMapper.insertHSfMaterials(GFGNEW);
        }
        log.info("新增主料、辅料和元素标准" +j +"条");
        log.info("更新主料、辅料和元素标准" +k +"条");
    }
    public void queryBeforeMaterias(String info,IoSession outputStream)  {
        String sub = info.substring(14);//010002004304F904F6FF 设备编码开始 之前去掉
        String type = hexToString(sub.substring(0, 8));//设备类型
        log.info("进入设备设置 ，设备类型：" + type);
        String equipNum = sub.substring(8,16);
        List<HSfAncillaryMaterias> hSfAncillaryMaterias = hSfAncillaryMateriasMapper
                .selectHSfAncillaryMateriasByEquipCode(type + equipNum);
        HashMap<String,HSfAncillaryMaterias> hSfAncillaryMateriasHashMap = new HashMap<>();
        for(HSfAncillaryMaterias ancillaryMaterias : hSfAncillaryMaterias){
            hSfAncillaryMateriasHashMap.put(ancillaryMaterias.geteName(),ancillaryMaterias);
        }
        List<HSfMaterials> hSfMaterials = hSfMaterialsMapper
                .selectHSfMaterialsByEquipCode(type + equipNum);
        HashMap<String,HSfMaterials> hSfMaterialsHashMap = new HashMap<>();
        for(HSfMaterials ancillaryMaterias : hSfMaterials){
            hSfMaterialsHashMap.put(ancillaryMaterias.getmName(),ancillaryMaterias);
        }
        String zjsStr16 = String2AcsII16.string2hex(KDTC) + equipNum + "00000000";
        HSfMaterials HL = hSfMaterialsHashMap.get("HL");
        if(HL != null){
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("HLC",8);
            zjsStr16 = zjsStr16 + "0000";
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(HL.getMC().multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("HLSi",8);
            zjsStr16 = zjsStr16 + "0000";
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(HL.getmSi().multiply(bigDecimal100).intValue());
        }else {
            log.error("HL信息缺失");
            return;
        }
        HSfMaterials FG = hSfMaterialsHashMap.get("FG");
        if(FG!= null){
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("FGC",8);
            zjsStr16 = zjsStr16 + "0000";
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(FG.getMC().multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("FGSi",8);
            zjsStr16 = zjsStr16 + "0000";
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(FG.getmSi().multiply(bigDecimal100).intValue());
        }else {
            log.error("FG信息缺失");
            return;
        }
        HSfMaterials ST =  hSfMaterialsHashMap.get("ST");
        if(ST!= null){
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("STC",8);
            zjsStr16 = zjsStr16 + "0000";
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(ST.getMC().multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("STSi",8);
            zjsStr16 = zjsStr16 + "0000";
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(ST.getmSi().multiply(bigDecimal100).intValue());
        }else {
            log.error("ST信息缺失");
            return;
        }
        EquipmentDetectionStandard equipmentDetectionStandard = equipmentDetectionStandardMapper
                .selectEquipmentDetectionStandardByEquipCode(type + equipNum);
        if(equipmentDetectionStandard == null){
            log.error(type + equipNum + "设备标准缺失！");
            return;
        }else {
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("BZC",8);
            zjsStr16 = zjsStr16 + "0000";
            zjsStr16 = zjsStr16 + int10to16_4(new BigDecimal(equipmentDetectionStandard.getC()).multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("BZSi",8);
            zjsStr16 = zjsStr16 + "0000";
            zjsStr16 = zjsStr16 + int10to16_4(new BigDecimal(equipmentDetectionStandard.getSi()).multiply(bigDecimal100).intValue());
        }

        HSfAncillaryMaterias SiCC = hSfAncillaryMateriasHashMap.get("SiCC");
        if(SiCC != null){
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("SiCC",8);
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(SiCC.geteAbsorb().multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(SiCC.geteEffective().multiply(bigDecimal100).intValue());
        }else {
            log.error("辅料碳化硅SiCC元素信息缺失");
            return;
        }
        HSfAncillaryMaterias SiCS = hSfAncillaryMateriasHashMap.get("SiCS");
        if(SiCS != null){
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("SiCS",8);
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(SiCS.geteAbsorb().multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(SiCS.geteEffective().multiply(bigDecimal100).intValue());
        }else {
            log.error("辅料硫化硅SiCS元素信息缺失");
            return;
        }

        HSfAncillaryMaterias SLC = hSfAncillaryMateriasHashMap.get("SLC");
        if(SLC != null){
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("SLC",8);
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(SLC.geteAbsorb().multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(SLC.geteEffective().multiply(bigDecimal100).intValue());
        }else {
            log.error("辅料SLC增碳剂信息缺失");
            return;
        }

        HSfAncillaryMaterias SLSi = hSfAncillaryMateriasHashMap.get("SLSi");
        if(SLSi != null){
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("SLSi",8);
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(SLSi.geteAbsorb().multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(SLSi.geteEffective().multiply(bigDecimal100).intValue());
        }else {
            log.error("辅料SLSi硅粒元素信息缺失");
            return;
        }
//        GFGC
        HSfMaterials GFG =  hSfMaterialsHashMap.get("GFG");
        if(GFG!= null){
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("GFGC",8);
            zjsStr16 = zjsStr16 + "0000";
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(GFG.getMC().multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("GFGS",8);
            zjsStr16 = zjsStr16 + "0000";
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(GFG.getmSi().multiply(bigDecimal100).intValue());
        }else {
            log.error("主料高硅废钢GFG信息缺失");
            return;
        }

        zjsStr16  = zjsStr16 + "00000000" + "00000000";
        int zjs = zjsStr16.length() / 2;// 不合格元素个数 * 4，再上位机收到光谱数转16进制
        String zjs16 = String2AcsII16.int10to16_2(zjs);

        String crc_str = "0103" + zjs16 + zjsStr16;
        String CRC_CHECK_CODE = CRC16Checksum.getCrc16ModBus(crc_str);//检验码

        StringBuffer sf = new StringBuffer();
        sf.append("FE");
        sf.append(crc_str);
        sf.append(CRC_CHECK_CODE);
        sf.append("FF");
        log.info("发送产前主料信息结果：" + sf.toString());
        byte[] byteArray = HexStrToByteArray(sf.toString());

//            outputStream.write(byteArray);
            sendMsg(outputStream,sf.toString());
       // outputStream.flush();
    }
    /**
     * 更新元素标准
     * @param info
     * @param outputStream
     * @
     */
    public void updateElements(String info,IoSession outputStream)  {
        String sub = info.substring(18);//010002004304F904F6FF 设备编码开始 之前去掉
        String type = hexToString(sub.substring(0, 8));//设备类型
        log.info("进入设备设置元素，设备类型：" + type);
        String equipNum = sub.substring(8,16);
//        byte[] byteArray = HexStrToByteArray("");

//            outputStream.write(byteArray);
            sendMsg(outputStream,"FE01100400002CE4C0FF");
       // outputStream.flush();
        sub = sub.substring(16);//用完16截取，继续0开始 保留以后得字符串

        String preview = sub.substring(0, 8);
        sub = sub.substring(8);//用完截取，继续0开始

        String C = hexToString(sub.substring(0,8));
        sub = sub.substring(8);//用完截取，继续0开始
        int C_sorb = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);//用完截取，继续0开始
        int C_eff = hex2Int(sub.substring(0,4));
        sub = sub.substring(4);//用完截取，继续0开始

        String Si = hexToString(sub.substring(0, 8));
        sub = sub.substring(8);//用完截取，继续0开始
        int Si_sorb = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);//用完截取，继续0开始
        int Si_eff = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);//用完16截取，继续0开始

        String Mn = hexToString(sub.substring(0, 8));
        sub = sub.substring(8);//用完16截取，继续0开始
        int Mn_sorb = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);//用完16截取，继续0开始
        int Mn_eff = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);//用完16截取，继续0开始

        String Sn = hexToString(sub.substring(0, 8));
        sub = sub.substring(8);//用完16截取，继续0开始
        int Sn_sorb = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);//用完16截取，继续0开始
        int Sn_eff = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);//用完16截取，继续0开始

        String Cu = hexToString(sub.substring(0, 8));
        sub = sub.substring(8);//用完16截取，继续0开始
        int Cu_sorb = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);//用完16截取，继续0开始
        int Cu_eff = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);//用完16截取，继续0开始

        String S = hexToString(sub.substring(0, 8));
        sub = sub.substring(8);//用完16截取，继续0开始
        int S_sorb = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);//用完16截取，继续0开始
        int S_eff = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);//用完16截取，继续0开始

        String Cr = hexToString(sub.substring(0, 8));
        sub = sub.substring(8);//用完16截取，继续0开始
        int Cr_sorb = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);//用完16截取，继续0开始
        int Cr_eff = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);//用完16截取，继续0开始

        String Al = hexToString(sub.substring(0, 8));
        sub = sub.substring(8);//用完16截取，继续0开始
        int Al_sorb = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);//用完16截取，继续0开始
        int Al_eff = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);//用完16截取，继续0开始

        String Ni = hexToString(sub.substring(0, 8));
        sub = sub.substring(8);//用完16截取，继续0开始
        int Ni_sorb = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);//用完16截取，继续0开始
        int Ni_eff = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);//用完16截取，继续0开始

        List<HSfAncillaryMaterias> hSfAncillaryMaterias = hSfAncillaryMateriasMapper
                .selectHSfAncillaryMateriasByEquipCode(type + equipNum);
        HashMap<String,HSfAncillaryMaterias> hSfAncillaryMateriasHashMap = new HashMap<>();
        for(HSfAncillaryMaterias ancillaryMaterias : hSfAncillaryMaterias){
            hSfAncillaryMateriasHashMap.put(ancillaryMaterias.geteName(),ancillaryMaterias);
        }
        BigDecimal b100 = new BigDecimal(100);
        int k = 0;
        int j = 0;
        HSfAncillaryMaterias co = hSfAncillaryMateriasHashMap.get("C");
        if(co != null){
            co.seteAbsorb(new BigDecimal(C_sorb).divide(b100));
            co.seteEffective(new BigDecimal(C_eff).divide(b100));
            k = k + hSfAncillaryMateriasMapper.updateHSfAncillaryMaterias(co);
            log.info("更新C");
        }else {
            HSfAncillaryMaterias co_new = new HSfAncillaryMaterias();
            co_new.seteName("C");
            co_new.setEquipType(type);
            co_new.setEquipNum(equipNum);
            co_new.setEquipCode(type + equipNum);
            co_new.seteAbsorb(new BigDecimal(C_sorb).divide(b100));
            co_new.seteEffective(new BigDecimal(C_eff).divide(b100));
            j = j + hSfAncillaryMateriasMapper.insertHSfAncillaryMaterias(co_new);
            log.info("写入C");
        }

        HSfAncillaryMaterias Sio = hSfAncillaryMateriasHashMap.get("Si");
        if(Sio != null){
            Sio.seteAbsorb(new BigDecimal(Si_sorb).divide(b100));
            Sio.seteEffective(new BigDecimal(Si_eff).divide(b100));
            k = k + hSfAncillaryMateriasMapper.updateHSfAncillaryMaterias(Sio);
            log.info("更新Si");
        }else {
            HSfAncillaryMaterias Sio_new = new HSfAncillaryMaterias();
            Sio_new.seteName("Si");
            Sio_new.setRemark(WarnDist.getDistYs().get("Si"));
            Sio_new.setEquipType(type);
            Sio_new.setEquipNum(equipNum);
            Sio_new.setEquipCode(type + equipNum);
            Sio_new.seteAbsorb(new BigDecimal(Si_sorb).divide(b100));
            Sio_new.seteEffective(new BigDecimal(Si_eff).divide(b100));
            j = j + hSfAncillaryMateriasMapper.insertHSfAncillaryMaterias(Sio_new);
            log.info("写入Si");
        }

        HSfAncillaryMaterias Mno = hSfAncillaryMateriasHashMap.get("Mn");
        if(Mno != null){
            Mno.seteAbsorb(new BigDecimal(Mn_sorb).divide(b100));
            Mno.seteEffective(new BigDecimal(Mn_eff).divide(b100));
            k = k + hSfAncillaryMateriasMapper.updateHSfAncillaryMaterias(Mno);
            log.info("更新Mn");
        }else {
            HSfAncillaryMaterias Mno_new = new HSfAncillaryMaterias();
            Mno_new.seteName("Mn");
            Mno_new.setRemark(WarnDist.getDistYs().get("Mn"));
            Mno_new.setEquipType(type);
            Mno_new.setEquipNum(equipNum);
            Mno_new.setEquipCode(type + equipNum);
            Mno_new.seteAbsorb(new BigDecimal(Mn_sorb).divide(b100));
            Mno_new.seteEffective(new BigDecimal(Mn_eff).divide(b100));
            j = j + hSfAncillaryMateriasMapper.insertHSfAncillaryMaterias(Mno_new);
        }

        HSfAncillaryMaterias Sno = hSfAncillaryMateriasHashMap.get("Sn");
        if(Sno != null){
            Sno.seteAbsorb(new BigDecimal(Sn_sorb).divide(b100));
            Sno.seteEffective(new BigDecimal(Sn_eff).divide(b100));
            k = k + hSfAncillaryMateriasMapper.updateHSfAncillaryMaterias(Sno);
        }else {
            HSfAncillaryMaterias Sno_new = new HSfAncillaryMaterias();
            Sno_new.seteName("Sn");
            Sno_new.setEquipType(type);
            Sno_new.setRemark(WarnDist.getDistYs().get("Sn"));
            Sno_new.setEquipNum(equipNum);
            Sno_new.setEquipCode(type + equipNum);
            Sno_new.seteAbsorb(new BigDecimal(Sn_sorb).divide(b100));
            Sno_new.seteEffective(new BigDecimal(Sn_eff).divide(b100));
            j = j + hSfAncillaryMateriasMapper.insertHSfAncillaryMaterias(Sno_new);
        }

        HSfAncillaryMaterias Cuo = hSfAncillaryMateriasHashMap.get("Cu");
        if(Cuo != null){
            Cuo.seteAbsorb(new BigDecimal(Cu_sorb).divide(b100));
            Cuo.seteEffective(new BigDecimal(Cu_eff).divide(b100));
            k = k + hSfAncillaryMateriasMapper.updateHSfAncillaryMaterias(Cuo);
        }else {
            HSfAncillaryMaterias Cuo_new = new HSfAncillaryMaterias();
            Cuo_new.seteName("Cu");
            Cuo_new.setRemark(WarnDist.getDistYs().get("Cu"));
            Cuo_new.setEquipType(type);
            Cuo_new.setEquipNum(equipNum);
            Cuo_new.setEquipCode(type + equipNum);
            Cuo_new.seteAbsorb(new BigDecimal(Cu_sorb).divide(b100));
            Cuo_new.seteEffective(new BigDecimal(Cu_eff).divide(b100));
            j = j +  hSfAncillaryMateriasMapper.insertHSfAncillaryMaterias(Cuo_new);
        }
        HSfAncillaryMaterias So = hSfAncillaryMateriasHashMap.get("S");
        if(So != null){
            So.seteAbsorb(new BigDecimal(S_sorb).divide(b100));
            So.seteEffective(new BigDecimal(S_eff).divide(b100));
            k = k + hSfAncillaryMateriasMapper.updateHSfAncillaryMaterias(So);
        }else {
            HSfAncillaryMaterias So_new = new HSfAncillaryMaterias();
            So_new.seteName("S");
            So_new.setRemark(WarnDist.getDistYs().get("S"));
            So_new.setEquipType(type);
            So_new.setEquipNum(equipNum);
            So_new.setEquipCode(type + equipNum);
            So_new.seteAbsorb(new BigDecimal(S_sorb).divide(b100));
            So_new.seteEffective(new BigDecimal(S_eff).divide(b100));
            j = j + hSfAncillaryMateriasMapper.insertHSfAncillaryMaterias(So_new);
        }

        HSfAncillaryMaterias Cro = hSfAncillaryMateriasHashMap.get("Cr");
        if(Cro != null){
            Cro.seteAbsorb(new BigDecimal(Cr_sorb).divide(b100));
            Cro.seteEffective(new BigDecimal(Cr_eff).divide(b100));
            k = k +  hSfAncillaryMateriasMapper.updateHSfAncillaryMaterias(Cro);
        }else {
            HSfAncillaryMaterias Cro_new = new HSfAncillaryMaterias();
            Cro_new.seteName("Cr");
            Cro_new.setRemark(WarnDist.getDistYs().get("Cr"));
            Cro_new.setEquipType(type);
            Cro_new.setEquipNum(equipNum);
            Cro_new.setEquipCode(type + equipNum);
            Cro_new.seteAbsorb(new BigDecimal(Cr_sorb).divide(b100));
            Cro_new.seteEffective(new BigDecimal(Cr_eff).divide(b100));
            j = j + hSfAncillaryMateriasMapper.insertHSfAncillaryMaterias(Cro_new);
        }

        HSfAncillaryMaterias Alo = hSfAncillaryMateriasHashMap.get("Al");
        if(Alo != null){
            Alo.seteAbsorb(new BigDecimal(Al_sorb).divide(b100));
            Alo.seteEffective(new BigDecimal(Al_eff).divide(b100));
            k = k + hSfAncillaryMateriasMapper.updateHSfAncillaryMaterias(Alo);
        }else {
            HSfAncillaryMaterias Alo_new = new HSfAncillaryMaterias();
            Alo_new.seteName("Al");
            Alo_new.setRemark(WarnDist.getDistYs().get("Al"));
            Alo_new.setEquipType(type);
            Alo_new.setEquipNum(equipNum);
            Alo_new.setEquipCode(type + equipNum);
            Alo_new.seteAbsorb(new BigDecimal(Al_sorb).divide(b100));
            Alo_new.seteEffective(new BigDecimal(Al_eff).divide(b100));
            j = j + hSfAncillaryMateriasMapper.insertHSfAncillaryMaterias(Alo_new);
        }

        HSfAncillaryMaterias Nio = hSfAncillaryMateriasHashMap.get("Ni");
        if(Nio != null){
            Nio.seteAbsorb(new BigDecimal(Ni_sorb).divide(b100));
            Nio.seteEffective(new BigDecimal(Ni_eff).divide(b100));
            k = k + hSfAncillaryMateriasMapper.updateHSfAncillaryMaterias(Nio);
        }else {
            HSfAncillaryMaterias Nio_new = new HSfAncillaryMaterias();
            Nio_new.seteName("Ni");
            Nio_new.setRemark(WarnDist.getDistYs().get("Ni"));
            Nio_new.setEquipType(type);
            Nio_new.setEquipNum(equipNum);
            Nio_new.setEquipCode(type + equipNum);
            Nio_new.seteAbsorb(new BigDecimal(Ni_sorb).divide(b100));
            Nio_new.seteEffective(new BigDecimal(Ni_eff).divide(b100));
            j = j + hSfAncillaryMateriasMapper.insertHSfAncillaryMaterias(Nio_new);
        }
        log.info("更新辅料" + k +" 条");
        log.info("更新辅料" + j +" 条");
    }
    public void queryElements(String info,IoSession outputStream)  {
        String sub = info.substring(14);//010002004304F904F6FF 设备编码开始 之前去掉
        String type = hexToString(sub.substring(0, 8));//设备类型
        log.info("进入设备设置 ，设备类型：" + type);
        String equipNum = sub.substring(8,16);
        List<HSfAncillaryMaterias> hSfAncillaryMaterias = hSfAncillaryMateriasMapper
                .selectHSfAncillaryMateriasByEquipCode(type + equipNum);
        HashMap<String,HSfAncillaryMaterias> hSfAncillaryMateriasHashMap = new HashMap<>();
        for(HSfAncillaryMaterias ancillaryMaterias : hSfAncillaryMaterias){
            hSfAncillaryMateriasHashMap.put(ancillaryMaterias.geteName(),ancillaryMaterias);
        }

        String zjsStr16 = String2AcsII16.string2hex(KDTC) + equipNum + "00000000";

        HSfAncillaryMaterias cobj = hSfAncillaryMateriasHashMap.get("C");
        if(cobj != null){
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("C",8);
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(cobj.geteAbsorb().multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(cobj.geteEffective().multiply(bigDecimal100).intValue());
        }else {
            log.error("C元素信息缺失");
            return;
        }
        HSfAncillaryMaterias siobj = hSfAncillaryMateriasHashMap.get("Si");
        if(siobj != null){
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("Si",8);
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(siobj.geteAbsorb().multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(siobj.geteEffective().multiply(bigDecimal100).intValue());
        }else {
            log.error("Si元素信息缺失");
            return;
        }
        HSfAncillaryMaterias mnobj = hSfAncillaryMateriasHashMap.get("Mn");
        if(mnobj != null){
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("Mn",8);
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(mnobj.geteAbsorb().multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(mnobj.geteEffective().multiply(bigDecimal100).intValue());
        }else {
            log.error("Mn元素信息缺失");
            return;
        }
        HSfAncillaryMaterias snobj = hSfAncillaryMateriasHashMap.get("Sn");
        if(snobj != null){
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("Sn",8);
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(snobj.geteAbsorb().multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(snobj.geteEffective().multiply(bigDecimal100).intValue());
        }else {
            log.error("Sn元素信息缺失");
            return;
        }
        HSfAncillaryMaterias cuobj = hSfAncillaryMateriasHashMap.get("Cu");
        if(cuobj != null){
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("Cu",8);
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(cuobj.geteAbsorb().multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(cuobj.geteEffective().multiply(bigDecimal100).intValue());
        }else {
            log.error("Cu元素信息缺失");
            return;
        }
        HSfAncillaryMaterias sobj = hSfAncillaryMateriasHashMap.get("S");
        if(sobj != null){
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("S",8);
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(sobj.geteAbsorb().multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(sobj.geteEffective().multiply(bigDecimal100).intValue());
        }else {
            log.error("S素信息缺失");
            return;
        }
        HSfAncillaryMaterias crobj = hSfAncillaryMateriasHashMap.get("Cr");
        if(crobj != null){
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("Cr",8);
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(crobj.geteAbsorb().multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(crobj.geteEffective().multiply(bigDecimal100).intValue());
        }else {
            log.error("Cr元素信息缺失");
            return;
        }
        HSfAncillaryMaterias Alobj = hSfAncillaryMateriasHashMap.get("Al");
        if(Alobj != null){
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("Al",8);
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(Alobj.geteAbsorb().multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(Alobj.geteEffective().multiply(bigDecimal100).intValue());
        }else {
            log.error("Al元素信息缺失");
            return;
        }
        HSfAncillaryMaterias niobj = hSfAncillaryMateriasHashMap.get("Ni");
        if(niobj != null){
            zjsStr16 = zjsStr16 + String2AcsII16.string2hex("Ni",8);
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(niobj.geteAbsorb().multiply(bigDecimal100).intValue());
            zjsStr16 = zjsStr16 + String2AcsII16.int10to16_4(niobj.geteEffective().multiply(bigDecimal100).intValue());
        }else {
            log.error("Ni元素信息缺失");
            return;
        }
        zjsStr16  = zjsStr16 + "00000000";
        int zjs = zjsStr16.length() / 2;// 不合格元素个数 * 4，再上位机收到光谱数转16进制
        String zjs16 = String2AcsII16.int10to16_2(zjs);

        String crc_str = "0103" + zjs16 + zjsStr16;
        String CRC_CHECK_CODE = CRC16Checksum.getCrc16ModBus(crc_str);//检验码

        StringBuffer sf = new StringBuffer();
        sf.append("FE");
        sf.append(crc_str);
        sf.append(CRC_CHECK_CODE);
        sf.append("FF");
        log.info("发送辅料元素信息结果：" + sf.toString());
        byte[] byteArray = HexStrToByteArray(sf.toString());
//            outputStream.write(byteArray);
            sendMsg(outputStream,sf.toString());
       // outputStream.flush();
    }
    public void str16(String info, IoSession session)  {
        String equipType = hexToString(info.substring(0, 8));
        String equipNum = info.substring(8,16);
        String equipCode = equipType + equipNum;
//        String msgType = "心跳";
        if(equipType.equals("0000")){
//            System.out.println("心跳：" + info);
        }else if(equipType.equals("KDTC")){
//            msgType = "注册包";
//            System.out.println("注册包：" + info) ;
            poolsHash.put(equipCode,session);
            System.out.println("poolHash:" + poolsHash.size());
            if(poolsHash.get(equipCode) == null){
                firstConnect(equipCode,session);//第一次连接
                log.info("首次链接缓存中无数据，同步数据库");
                EquipmentConnectInfo equipmentConnectInfo1 = equipmentConnectInfoMapper
                        .selectEquipmentConnectInfoEquipCode(equipCode);
                if(equipmentConnectInfo1 != null){
                    log.info("缓存中无，数据库中有,更新缓存和数据");
                    poolsHash.put(equipCode,session);
                    equipmentConnectInfo1.setEquipType(equipType);
                    equipmentConnectInfo1.setEquipNum(equipNum);
                    equipmentConnectInfo1.setEquipCode(equipCode);
                    equipmentConnectInfo1.setIp(session.getRemoteAddress().toString());
                    int i = equipmentConnectInfoMapper.updateEquipmentConnectInfo(equipmentConnectInfo1);
                    log.info("更新" + i + "设备ip关联信息");
                }else {
                    log.info("缓存中無，数据库中也無,写入缓存和数据");
                    EquipmentConnectInfo equipmentConnectInfo = new EquipmentConnectInfo();
                    equipmentConnectInfo.setEquipType(equipType);
                    equipmentConnectInfo.setEquipNum(equipNum);
                    equipmentConnectInfo.setEquipCode(equipCode);
                    equipmentConnectInfo.setIp(session.getRemoteAddress().toString());
                    int i = equipmentConnectInfoMapper.insertEquipmentConnectInfo(equipmentConnectInfo);
                    log.info("写入设备ip关联信息" + i +"条");
                }
            }else if(!poolsHash.get(equipCode).equals(session)){
                log.info("设备ip有变更,更新连接缓存池");
                EquipmentConnectInfo equipmentConnectInfo = equipmentConnectInfoMapper
                        .selectEquipmentConnectInfoEquipCode(equipCode);
                if(equipmentConnectInfo != null){
                    equipmentConnectInfo.setEquipType(equipType);
                    equipmentConnectInfo.setEquipNum(equipNum);
                    equipmentConnectInfo.setEquipCode(equipCode);
                    equipmentConnectInfo.setIp(session.getRemoteAddress().toString());
                    int i = equipmentConnectInfoMapper.updateEquipmentConnectInfo(equipmentConnectInfo);
                    log.info("写入设备ip关联信息" + i +"条");
                }else {
                    EquipmentConnectInfo equipmentConnectInfo1 = new EquipmentConnectInfo();
                    equipmentConnectInfo1.setEquipType(equipType);
                    equipmentConnectInfo1.setEquipNum(equipNum);
                    equipmentConnectInfo1.setEquipCode(equipCode);
                    equipmentConnectInfo1.setIp(session.getRemoteAddress().toString());
                    int i = equipmentConnectInfoMapper.insertEquipmentConnectInfo(equipmentConnectInfo1);
                    log.info("更新设备ip关联信息" + i +"条");
                }
            }else {
                log.info("设备IP关联信息无变更");
            }
        }
        String responseStr = info;    //回复//响应内容
//        byte[] byteArray = HexStrToByteArray(responseStr);
//        sendMsg(session,responseStr);
        sendMsg(session,responseStr);
//        session.write(responseStr);
//        session.flush();
//        log.info("回应:" + responseStr);

    }
    public void updateEquipmentInfo(String info,IoSession outputStream)  {
        String sf = "FE01100300002C90C1FF";
        byte[] byteArray = HexStrToByteArray(sf);
//            outputStream.write(byteArray);
            sendMsg(outputStream,sf);
       // outputStream.flush();

        String sub = info.substring(18);//010002004304F904F6FF 设备编码开始 之前去掉
        String type = hexToString(sub.substring(0, 8));//设备类型
        log.info("进入设备设置 ，设备类型：" + type);
        String equipNum = sub.substring(8,16);
        sub = sub.substring(16);//用完16截取，继续0开始 保留以后得字符串
        sub = sub.substring(16);//用完16截取，继续0开始 保留以后得字符串
        String upModel = sub.substring(0, 8);
        //00000001是WIFI 2 是LORA 3 4G
        if(upModel.equals(UP_MODEL_WIFI)){
            upModel = "WIFI";
        }else if(upModel.equals(UP_MODEL_LORA)){
            upModel = "LORA";
        }else if(upModel.equals(UP_MODEL_4G)){
            upModel = "4G";
        }
        sub = sub.substring(8);//用完16截取，继续0开始
        String ip1 = hex2Int(sub.substring(0, 4)) + "";
        String ip2 = hex2Int(sub.substring(4, 8)) + "";
        String ip3 = hex2Int(sub.substring(8, 12)) + "";
        String ip4 = hex2Int(sub.substring(12, 16)) +"";
        String ip = ip1 + "." + ip2 + "." + ip3 + "." +ip4;
        sub = sub.substring(16);//用完16截取，继续0开始
        int port = hex2Int(sub.substring(0, 8));
        sub = sub.substring(8);//用完16截取，继续0开始
        int heart = hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);
        String scal = sub.substring(0, 4);
        //00000001是C3 2是C6
        if(scal.equals(SCAL_C3)){
            scal = "C3";
        }else if(scal.equals(SCAL_C6)){
            scal = "C6";
        }
        sub = sub.substring(4);
        int mil = hex2Int(sub.substring(0, 8));//量程
        sub = sub.substring(8);
        String imi = sub.substring(0, 32);//imie
        String imii_db = "";
        for (int i = 0; i < imi.length(); i = i + 2) {
            String s = int10to16_2(Integer.valueOf(imi.substring(i, i + 2)));
            s = hexToString(s);
            imii_db = imii_db + s;
        }
        sub = sub.substring(32);
        String cardNum = sub.substring(0, 40);
        EquipmentInfo equipmentInfo1 = equipmentInfoMapper.selectEquipmentInfoByEquipmentCode(type + equipNum);
        if(equipmentInfo1 != null){
            equipmentInfo1.setCardNum(cardNum);
            equipmentInfo1.setConMileage(Long.valueOf(mil));
            equipmentInfo1.setImie(imii_db);
            equipmentInfo1.setConHeart(Long.valueOf(heart));
            equipmentInfo1.setConIp(ip);
            equipmentInfo1.setConPort(port + "");
            equipmentInfo1.setConAcc(scal);//精度模式
            equipmentInfo1.setUpModel(upModel);
            equipmentInfo1.setEquipType(type);
            equipmentInfo1.setEquipNum(equipNum);
            equipmentInfo1.setEquipmentCode(type + equipNum);
            equipmentInfo1.setUpdateTime(new Date());
            int i = equipmentInfoMapper.updateEquipmentInfo(equipmentInfo1);
            log.info("更新设备信息" + i +"条");
        }else {
            EquipmentInfo equipmentInfo = new EquipmentInfo();
            equipmentInfo.setCardNum(cardNum);
            equipmentInfo.setImie(imii_db);
            equipmentInfo.setConHeart(Long.valueOf(heart));
            equipmentInfo.setConIp(ip);
            equipmentInfo.setConPort(port + "");
            equipmentInfo.setConAcc(scal);
            equipmentInfo.setEquipType(type);
            equipmentInfo.setEquipNum(equipNum);
            equipmentInfo.setEquipmentCode(type + equipNum);
            equipmentInfo.setCreateTime(new Date());
            int i = equipmentInfoMapper.insertEquipmentInfo(equipmentInfo);
            log.info("写入设备信息"+ i +"条");
        }
    }
    public void queryEquipmentInfo(String info,IoSession outputStream) {
        String sub = info.substring(14);//010002004304F904F6FF 设备编码开始 之前去掉
        String type = hexToString(sub.substring(0, 8));//设备类型
        log.info("进入设备设置 ，设备类型：" + type);
        String equipNum = sub.substring(8,16);
        sub = sub.substring(16);//用完16截取，继续0开始 保留以后得字符串
        EquipmentInfo equipmentInfo = equipmentInfoMapper.selectEquipmentInfoByEquipmentCode(type + equipNum);
        if(equipmentInfo != null){
            String equipType16 = String2AcsII16.string2hex(equipmentInfo.getEquipType());
            String equipNum10 = equipmentInfo.getEquipNum();
            String upModel16 = UP_MODEL_WIFI;
            if(equipmentInfo.getUpModel().equals("WIFI")){
                upModel16 = UP_MODEL_WIFI;
            }else if(equipmentInfo.getUpModel().equals("LARA")){
                upModel16 = UP_MODEL_LORA;
            }else if(equipmentInfo.getUpModel().equals("4G")){
                upModel16 = UP_MODEL_4G;
            }
            String conIp = equipmentInfo.getConIp();
            String[] split = conIp.split("\\.");
            String ip16 = String2AcsII16.int10to16_4(Integer.valueOf(split[0]))
                    + String2AcsII16.int10to16_4(Integer.valueOf(split[1]))
                    + String2AcsII16.int10to16_4(Integer.valueOf(split[2]))
                    + String2AcsII16.int10to16_4(Integer.valueOf(split[3])) ;

            String port16 = String2AcsII16.int10to16_4(Integer.valueOf(equipmentInfo.getConPort()));
            String heart16 = String2AcsII16.int10to16_4(equipmentInfo.getConHeart().intValue());

            String scal16 = equipmentInfo.getConAcc();//精度
            if(scal16.equals("C6")){
                scal16 = SCAL_C6;
            }else if(scal16.equals("C3")){
                scal16 = SCAL_C3;
            }
            String mil16 = String2AcsII16.int10to16_4(equipmentInfo.getConMileage().intValue());//量程
//            String imie = equipmentInfo.getImie();
            String imie = "00000000" + "00000000" + "00000000" + "00000000" ;
            String cardNum = equipmentInfo.getCardNum();
            String zjsStr16 = equipType16 + equipNum10  +"00000001" +"00000001"+  upModel16 + ip16 +"0000"  + port16 + heart16
                    + scal16 + "0000"+mil16 + imie + cardNum +"00000001" + "00000001" + "00000001" ;
            int zjs = zjsStr16.length() / 2;// 不合格元素个数 * 4，再上位机收到光谱数转16进制
            String zjs16 = String2AcsII16.int10to16_2(zjs);
            String modbus_addr_function_code = "0103";//Modbus地址
            String crc_str = modbus_addr_function_code + zjs16 + zjsStr16;
            String CRC_CHECK_CODE = CRC16Checksum.getCrc16ModBus(crc_str);//检验码

            StringBuffer sf = new StringBuffer();
            sf.append("FE");
            sf.append(crc_str);
            sf.append(CRC_CHECK_CODE);
            sf.append("FF");
            log.info("发送查询设备信息结果：" + sf.toString());
            byte[] byteArray = HexStrToByteArray(sf.toString());

//            outputStream.write(byteArray);
            sendMsg(outputStream,sf.toString());
           // outputStream.flush();
        }
    }
    public void updateEquipments(String info,IoSession outputStream){
        String responseStr = "FE01100200001C78C0FF";
        byte[] byteArray = HexStrToByteArray(responseStr);

//            outputStream.write(byteArray);
            sendMsg(outputStream,responseStr);
       // outputStream.flush();
        log.info(">>>进入设置设备编码回应线程:" + responseStr);

        String sub = info.substring(18);//010002004304F904F6FF 设备编码开始 之前去掉
        String type = hexToString(sub.substring(0, 8));//设备编码
        log.info("进入设置设备编码：" + type );
        String equipNum = sub.substring(8,16);
        sub = sub.substring(16);
        String lh1 = hexToString(sub.substring(0, 16));
        sub = sub.substring(16);
        String lh2 = hexToString(sub.substring(0, 16));
        sub = sub.substring(16);
        String lh3 = hexToString(sub.substring(0, 16));
        sub = sub.substring(16);
        String lh4 = hexToString(sub.substring(0, 16));
        sub = sub.substring(16);
        String lh5 = hexToString(sub.substring(0, 16));
        sub = sub.substring(16);
        String lh6 = hexToString(sub.substring(0, 16));
        log.info("收到炉号：" + lh1 +"&"+ lh2  + "&"+ lh3 +"&"+  lh4 + "&"+  lh5 + "&"+  lh6 +"设备编码"+equipNum
                +"设备类型" + type+ "设备名称" + type+equipNum );
        ScaleEquipmentMamage scaleEquipmentMamage = new ScaleEquipmentMamage();
        scaleEquipmentMamage.setEquName(type + equipNum);
        List<ScaleEquipmentMamage> list1 = scaleEquipmentMamageMapper.selectScaleEquipmentMamageList(scaleEquipmentMamage);
        int i = 0;
        for (ScaleEquipmentMamage equipmentMamage : list1) {
            i = i + scaleEquipmentMamageMapper.deleteScaleEquipmentMamageById(equipmentMamage.getId());
        }
        log.info("删除设备" + i + "条");
        String EQUIPMENT_CODE = type + equipNum;
        String equipCode = type + equipNum;
        String setEquNum = equipNum;
        String setType = type;
        List<ScaleEquipmentMamage> list = new ArrayList<>();
        String setEquName = EQUIPMENT_CODE;
        //1
        ScaleEquipmentMamage scaleEquipmentMamage1 = new ScaleEquipmentMamage();
        scaleEquipmentMamage1.setEquName(equipCode);
        scaleEquipmentMamage1.setEquNum(setEquNum);
        scaleEquipmentMamage1.setType(setType);
        scaleEquipmentMamage1.setLh(lh1);
        list.add(scaleEquipmentMamage1);
        //2
        ScaleEquipmentMamage scaleEquipmentMamage2 = new ScaleEquipmentMamage();
        scaleEquipmentMamage2.setEquName(equipCode);
        scaleEquipmentMamage2.setEquNum(setEquNum);
        scaleEquipmentMamage2.setType(setType);
        scaleEquipmentMamage2.setLh(lh2);
        list.add(scaleEquipmentMamage2);
        //3
        ScaleEquipmentMamage scaleEquipmentMamage3 = new ScaleEquipmentMamage();
        scaleEquipmentMamage3.setEquName(equipCode);
        scaleEquipmentMamage3.setEquNum(setEquNum);
        scaleEquipmentMamage3.setType(setType);
        scaleEquipmentMamage3.setLh(lh3);
        list.add(scaleEquipmentMamage3);

        ScaleEquipmentMamage scaleEquipmentMamage4 = new ScaleEquipmentMamage();
        scaleEquipmentMamage4.setEquName(equipCode);
        scaleEquipmentMamage4.setEquNum(setEquNum);
        scaleEquipmentMamage4.setType(setType);
        scaleEquipmentMamage4.setLh(lh4);
        list.add(scaleEquipmentMamage4);

        ScaleEquipmentMamage scaleEquipmentMamage5 = new ScaleEquipmentMamage();
        scaleEquipmentMamage5.setEquName(equipCode);
        scaleEquipmentMamage5.setEquNum(setEquNum);
        scaleEquipmentMamage5.setType(setType);
        scaleEquipmentMamage5.setLh(lh5);
        list.add(scaleEquipmentMamage5);

        ScaleEquipmentMamage scaleEquipmentMamage6 = new ScaleEquipmentMamage();
        scaleEquipmentMamage6.setEquName(equipCode);
        scaleEquipmentMamage6.setEquNum(setEquNum);
        scaleEquipmentMamage6.setType(setType);
        scaleEquipmentMamage6.setLh(lh6);
        list.add(scaleEquipmentMamage6);
        int j = 0;
        for (ScaleEquipmentMamage equipmentMamage : list) {
            j = j+ scaleEquipmentMamageMapper.insertScaleEquipmentMamage(equipmentMamage);
        }
        log.info("更新设备" + j + "条");
    }
    public void queryEquipments(String info,IoSession outputStream) {
        String sub = info.substring(14);//010002004304F904F6FF 设备编码开始 之前去掉
        String type = hexToString(sub.substring(0, 8));//设备编码
        log.info("进入设置设备编码：" + type );
        String equipNum = sub.substring(8,16);
        //返回设备查询消息
        log.info("进入查询炉次");
        log.info("设备编码" + type +  equipNum);
        ScaleEquipmentMamage scaleEquipmentMamage = new ScaleEquipmentMamage();
        scaleEquipmentMamage.setEquName(type + equipNum);
        List<ScaleEquipmentMamage> scaleEquipmentMamages = scaleEquipmentMamageMapper.selectScaleEquipmentMamageList(scaleEquipmentMamage);
        StringBuffer lhs = new StringBuffer();
        for (ScaleEquipmentMamage equipmentMamage : scaleEquipmentMamages) {
            String lh = String2AcsII16.string2hex(equipmentMamage.getLh().trim(),16);
            lhs.append(lh);//两位hex值
        }
        if (scaleEquipmentMamages.size() < 6) {
            int bw = 6 - scaleEquipmentMamages.size();
            for (int i = 0; i < bw; i++) {
                lhs.append("0000000000000000");//空炉号补16个0
            }
        }
        String zjsStr16 = String2AcsII16.string2hex(type,4) + equipNum + lhs ;
        int zjs = zjsStr16.length() / 2;// 不合格元素个数 * 4，再上位机收到光谱数转16进制
        String zjs16 = String2AcsII16.int10to16_2(zjs);
        String modbus_addr_function_code = "0103";//Modbus地址
        String crc_str = modbus_addr_function_code + zjs16 + zjsStr16;
        String CRC_CHECK_CODE = CRC16Checksum.getCrc16ModBus(crc_str);//检验码
        StringBuffer sf = new StringBuffer();
        sf.append("FE");
        sf.append(crc_str);
        sf.append(CRC_CHECK_CODE);
        sf.append("FF");
        log.info("发送查询设备信息结果：" + sf.toString());
        byte[] byteArray = HexStrToByteArray(sf.toString());

//            outputStream.write(byteArray);
            sendMsg(outputStream,sf.toString());
       // outputStream.flush();
        log.info(">>>查询炉次回应线程:" + sf.toString());
    }
    public void addFeeding(String info,IoSession outputStream) {
        String responseStr = "FE0110010000163B40FF";//FE0110010000163B40FF
//        byte[] byteArray = HexStrToByteArray(responseStr);
//            outputStream.write(byteArray);
            sendMsg(outputStream,responseStr);
       // outputStream.flush();
        DetectionRlEjrl detectionRlEjrl = new DetectionRlEjrl();
        String sub = info.substring(18);//010002004304F904F6FF 设备编码开始 之前去掉
        String type = hexToString(sub.substring(0, 8));//设备编码
        String equipNum = sub.substring(8, 16);//设备编码
        String equipCode = type + equipNum;
        sub = sub.substring(16);
        String lh = hexToString(sub.substring(0, 16));
        sub = sub.substring(16);
        String lch = hexToString(sub.substring(0, 24));
        sub = sub.substring(24);
        int tszl = String2AcsII16.hex2Int(sub.substring(0, 4));//铁水重
        sub = sub.substring(4);
        String elment = hexToString(sub.substring(0, 4));
        if(elment.equals("SC")){//转换SC => SiC
            elment = "SiC";
        }
        sub = sub.substring(4);
        int llz = String2AcsII16.hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);
        int sjz = String2AcsII16.hex2Int(sub.substring(0, 4));
        sub = sub.substring(4);
        String JLLX = sub.substring(0, 4);//加料类型 -- 产中产前 产后阶段
        String production_status_str  = "默认产前";
        if(JLLX.equals(NORMAL)){
            log.info("正常加料");
            production_status_str = "产中";
        }else if(JLLX.equals(GTJT)){
            production_status_str = "产中";
            log.info("高碳降碳");
        }else if(JLLX.equals(BEFOR)){
            production_status_str = "产前";
            log.info("产前");
        }else if(JLLX.equals(YYJ)){
            production_status_str = "产后";
            log.info("产后");
        }
        sub = sub.substring(4);
        String warn = sub.substring(0, 4);//warn
        if(JLLX.equals(ONLY_WARN)||JLLX.toUpperCase().equals(ONLY_WARN)){
            log.info("设备编码 ：" + equipCode +"单纯报警" + ONLY_WARN  + "报警" + warn);
            System.out.println("写入单纯报警信息一条0000");
            ScaleAlarmSub scaleAlarmSub = new ScaleAlarmSub();
            scaleAlarmSub.setAlarmType(ONLY_WARN);
            scaleAlarmSub.setAlarmMsg(WarnDist.getDist().get(warn));
            scaleAlarmSub.setEquNum(equipCode);
            scaleAlarmSub.setRetain2(WarnDist.getDistJjfa().get(warn));
            int i = scaleAlarmSubMapper.insertScaleAlarmSub(scaleAlarmSub);
            log.info("写入报警子表加入量 " + i +"条净值单纯报警：");
            log.info(scaleAlarmSub.toString());
        }else {
            log.info("加料 && || 报警");
            detectionRlEjrl.setLh(lh);
            detectionRlEjrl.setLch(lch);
            detectionRlEjrl.setElementType(elment);
            detectionRlEjrl.setLlz(new BigDecimal(llz).divide(new BigDecimal(100), 3, BigDecimal.ROUND_UP) + "");
            detectionRlEjrl.setSjz(new BigDecimal(sjz).divide(new BigDecimal(100), 3, BigDecimal.ROUND_UP) + "");
            detectionRlEjrl.setCreateBy("tcpSever");
            int i1 = detectionRlEjrlMapper.insertDetectionRlEjrl(detectionRlEjrl);
            System.out.println("写入detectionRlEjrl辅料加入量 " + i1 +"条。" + detectionRlEjrl.toString());
            //查询主表信息
            Long masterId = null;
            log.info("lh:" + lh + "lch:" + lch);
            log.info("lh + lch：" + lh + "| " + lch);
            DetectionRl detectionRl = detectionRlMapper.getDetectionByLch(lh + lch);
            ScaleAlarmMaster scaleAlarmMaster1 = scaleAlarmMasterMapper.selectScaleAlarmMasterByLchLimit2Hour(lh, lch);
            if (scaleAlarmMaster1 == null) {
                ScaleAlarmMaster scaleAlarmMaster = new ScaleAlarmMaster();
                if (warn.equals("0000")) {// //未报警不再处理 此字段
                    scaleAlarmMaster.setAlarmType(WarnDist.qual);//直接存信息吧 好查，子表存全的
                    scaleAlarmMaster.setRetain2("加料");
                } else {
                    scaleAlarmMaster.setAlarmType(WarnDist.qual_no);
                }
                if (detectionRl != null) {
                    scaleAlarmMaster.setPh(detectionRl.getMarkId());
                } else {
                    log.error("质检数据无，不能再主表中存牌号！");
                }
                scaleAlarmMaster.setLh(lh);
                scaleAlarmMaster.setLch(lch);
                scaleAlarmMaster.setEquNum(equipCode);
                scaleAlarmMaster.setRetain2("已加料");//是否加料
                scaleAlarmMaster.setRetain1(tszl + "");//铁水重
                scaleAlarmMaster.setCreateTime(new Date());
                scaleAlarmMaster.setCreateUser("tcpServer");
                int i = scaleAlarmMasterMapper.insertScaleAlarmMaster(scaleAlarmMaster);
                masterId = scaleAlarmMaster.getId();//自增ID
                log.info("写入主表" + i +"条：");
                log.info(scaleAlarmMaster.toString());
            } else {
                masterId = scaleAlarmMaster1.getId();
                if (!warn.equals("0000")) {// //未报警不再处理 此字段
                    scaleAlarmMaster1.setAlarmType(WarnDist.qual_no);//直接存信息吧 好查，子表存全的
                }
                ScaleAlarmSub scaleAlarmSub = new ScaleAlarmSub();
                scaleAlarmSub.setMasterId(masterId);
                List<ScaleAlarmSub> scaleAlarmSubs = scaleAlarmSubMapper.selectScaleAlarmSubList(scaleAlarmSub);
                if(scaleAlarmSubs != null || scaleAlarmSubs.size() > 0){
                    Date createTime = scaleAlarmSubs.get(0).getCreateTime();
                    if(createTime.getTime() > (scaleAlarmMaster1.getCreateTime().getTime() + 40 * 60 * 1000)){
//                        1、加料超过40分钟  --  创建时间
                        scaleAlarmMaster1.setAlarmType(WarnDist.qual_no);
                    }
                }
                scaleAlarmMaster1.setRetain2("加料");//是否加料
                scaleAlarmMaster1.setUpdateTime(new Date());
                scaleAlarmMaster1.setCreateTime(new Date());
                int i = scaleAlarmMasterMapper.updateScaleAlarmMaster(scaleAlarmMaster1);
                log.info("主表信息已存在，更新主表信息" +  i + "条");
            }
            //加料   &&  加料报警
            ScaleAlarmSub scaleAlarmSub = new ScaleAlarmSub();
            scaleAlarmSub.setAlarmType(warn);
            scaleAlarmSub.setMasterId(masterId);
            scaleAlarmSub.setAlarmMsg(WarnDist.getDist().get(warn));
            if (detectionRl != null && production_status_str.equals("产中") ) {
                String markId = detectionRl.getMarkId();
                String plant = detectionRl.getPlant();
                String factoryCode = plant.split("-")[1];
                log.info("查询标准mark:" + markId);
                log.info("车间:" + factoryCode);
                SmelterMelterStandard bySmelterNum = smelterMelterStandardMapper.getBySmelterNum(markId,factoryCode);
                if (bySmelterNum != null) {
                    if (elment.equals("C")) {
                        scaleAlarmSub.setMax(new BigDecimal(bySmelterNum.getcMax()));
                        scaleAlarmSub.setMin(new BigDecimal(bySmelterNum.getcMin()));
                        scaleAlarmSub.setRetain1("增碳剂");
                        scaleAlarmSub.setPrValue(new BigDecimal(detectionRl.getC()));
                    } else if (elment.equals("Si")) {
                        scaleAlarmSub.setMax(new BigDecimal(bySmelterNum.getSiMax()));
                        scaleAlarmSub.setMin(new BigDecimal(bySmelterNum.getSiMin()));
                        scaleAlarmSub.setRetain1("硅铁");
                        scaleAlarmSub.setPrValue(new BigDecimal(detectionRl.getSi()));
                    } else if (elment.equals("Mn")) {
                        scaleAlarmSub.setMax(new BigDecimal(bySmelterNum.getMnMax()));
                        scaleAlarmSub.setMin(new BigDecimal(bySmelterNum.getMnMin()));
                        scaleAlarmSub.setRetain1("锰铁");
                        scaleAlarmSub.setPrValue(new BigDecimal(detectionRl.getMn()));
                    }
                } else {
                    log.error("牌号：" + markId + "标准缺失，不再附带上限下限");
                }
            } else {
                log.error("无关联质检数据，是否停产很长时间！");
            }
            scaleAlarmSub.setAlarmType(warn);
            if (!warn.equals("0000")) {// //未报警不再处理 此字段
                scaleAlarmSub.setAlarmMsg(WarnDist.getDist().get(warn));
            } else {
                scaleAlarmSub.setAlarmMsg("无报警");
            }
             //增碳剂通过C Si Mn S AL Ni SC Cr Sn
            scaleAlarmSub.setEle(elment);//元素名称
            scaleAlarmSub.setRetain1( WarnDist.getDistYs().get(elment));//辅料名称
            scaleAlarmSub.setLch(lch);
            scaleAlarmSub.setLh(lh);
            scaleAlarmSub.setEquNum(equipCode);
            scaleAlarmSub.setActualm(new BigDecimal(sjz).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP));
            scaleAlarmSub.setCountm(new BigDecimal(llz).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP));
            scaleAlarmSub.setRetain1(WarnDist.getDistYs().get(elment));
            scaleAlarmSub.setRetain3(production_status_str);//产前产中产后
            scaleAlarmSub.setCreateTime(new Date());
            int i = scaleAlarmSubMapper.insertScaleAlarmSub(scaleAlarmSub);
            log.info("写入报警子表加入量" + i +"条：");
            log.info(scaleAlarmSub.toString());
        }
    }
    public void firstConnect(String EQUIPMENT_CODE,IoSession outputStream)  {
        if (isFirstConnect) {
            ScaleEquipmentMamage scaleEquipmentMamage = new ScaleEquipmentMamage();
            scaleEquipmentMamage.setEquName(EQUIPMENT_CODE);
            List<ScaleEquipmentMamage> scaleEquipmentMamages = scaleEquipmentMamageMapper.selectScaleEquipmentMamageList(scaleEquipmentMamage);
            StringBuffer lhs = new StringBuffer();
            for (ScaleEquipmentMamage equipmentMamage : scaleEquipmentMamages) {
                String lh = String2AcsII16.string2hex(equipmentMamage.getLh().trim(),16);
                lhs.append(lh);//两位hex值
            }
            if (scaleEquipmentMamages.size() < 6) {
                int bw = 6 - scaleEquipmentMamages.size();
                for (int i = 0; i < bw; i++) {
                    lhs.append("0000000000000000");//空炉号补16个0
                }
            }
            String head = "01100200";
            log.info("设备编码长度：" + EQUIPMENT_CODE.length());
            String jcqStr = EQUIPMENT_CODE + lhs.toString();
            log.info("128计算寄存器的内容总长度：" + jcqStr.length());
            int jcq = jcqStr.length() / 4;// 不合格元素个数 * 4，再上位机收到光谱数转16进制
            int zjs = jcq * 2;//寄存器数目*2再转16进制
            String crcStr = head + String2AcsII16.int10to16_4(jcq) + String2AcsII16.int10to16_4(zjs) + jcqStr;
            String crc = CRC16Checksum.getCrc16ModBus(crcStr);//CRC
            String responseStr = "FE" + crcStr + crc + "FF";//设备列表 mapper//TODO设备列表mysql
            isFirstConnect = false;
            log.info("第一次链接，设置炉号中。。。");
            byte[] byteArray = HexStrToByteArray(responseStr);
//            outputStream.write(byteArray);
            sendMsg(outputStream,responseStr);
//            sendMsg(outputStream,responseStr);
//           // outputStream.flush();
            log.info("回应线程:" + responseStr);
        }
    }
    public static void sendMsg(IoSession session,String msg) {
        byte[] byteArray = NumberUtils.hexStringToByteArray(msg);//发送hex串转byte
            session.write(IoBuffer.wrap(byteArray));
    }
//        public static void sendMsg(IoSession session,byte[] msg) {
//        try {
//            session.write(msg);
//        }catch(Exception e) {
//            System.out.println("sendMsg error: "+e.getMessage());
//            e.printStackTrace();
//        }
//    }
}
